package com.avic.rong.userOrder.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.userOrder.api.AddOrUpdateInvoiceApi;
import com.avic.rong.userOrder.api.GetHistoryInvoiceApi;
import com.avic.rong.userOrder.api.InvoiceDetailByTargetIdApi;
import com.avic.rong.userOrder.api.ZengZhiShuiApi;
import com.avic.rong.userOrder.bean.FaPiaoDetailsBean;
import com.avic.rong.userOrder.bean.GetComanyInvoiceBean;
import com.avic.rong.userOrder.bean.ZengZhiShuiBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import butterknife.BindView;

public class UpdateFaPiaoActivity extends BaseActivity {


    @BindView(R.id.ll_dianzifapiao_type)
    LinearLayout ll_dianzifapiao_type;
    @BindView(R.id.ll_company_name)
    LinearLayout llCompanyName;
    @BindView(R.id.ll_company_shuihao)
    LinearLayout llCompanyShuihao;
    @BindView(R.id.ll_company)
    LinearLayout llCompany;
    @BindView(R.id.tv_submit_order)
    TextView tvSubmitOrder;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;
    @BindView(R.id.et_fapiao_sn)
    EditText et_fapiao_sn;
    @BindView(R.id.et_fapiao_type)
    EditText et_fapiao_type;
    @BindView(R.id.et_order_content)
    EditText etOrderContent;
    @BindView(R.id.tv_fapiao_taitou)
    TextView tvFapiaoTaitou;
    @BindView(R.id.et_company_name)
    EditText etCompanyName;
    @BindView(R.id.et_shui_tel)
    EditText etShuiTel;
    @BindView(R.id.et_phone_tel)
    EditText etPhoneTel;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_company_address)
    EditText etCompanyAddress;
    @BindView(R.id.ll_company_address)
    LinearLayout llCompanyAddress;
    @BindView(R.id.et_bank_tel)
    EditText etBankTel;
    @BindView(R.id.ll_bank_tel)
    LinearLayout llBankTel;
    @BindView(R.id.ll_fapiao_type)
    LinearLayout ll_fapiao_type;
    @BindView(R.id.ll_fapiao_content)
    LinearLayout ll_fapiao_content;
    @BindView(R.id.tv_fapiao_type)
    TextView tv_fapiao_type;
    @BindView(R.id.tv_fapiao_message)
    TextView tv_fapiao_message;
    @BindView(R.id.et_regist_address_zengzhi)
    EditText etRegistAddressZengzhi;
    @BindView(R.id.ll_regist_address_zengzhi)
    LinearLayout llRegistAddressZengzhi;
    @BindView(R.id.et_regist_phone_zengzhi)
    EditText etRegistPhoneZengzhi;
    @BindView(R.id.ll_regist_phone_zengzhi)
    LinearLayout llRegistPhoneZengzhi;
    @BindView(R.id.ll_address_phone_zengzhi)
    LinearLayout llAddressPhoneZengzhi;
    @BindView(R.id.et_bank_name_zengzhi)
    EditText etBankNameZengzhi;
    @BindView(R.id.ll_bank_name_zengzhi)
    LinearLayout llBankNameZengzhi;
    @BindView(R.id.et_bank_tel_zengzhi)
    EditText etBankTelZengzhi;
    @BindView(R.id.ll_bank_tel_zengzhi)
    LinearLayout llBankTelZengzhi;
    @BindView(R.id.ll_bank_banktel_zengzhi)
    LinearLayout llBankBanktelZengzhi;
    private PopupWindow popupWindow;
    private String order_id;
    private String order_type;
    private String order_sn;
    private View fapiaoMessageView;
    private RelativeLayout rl_close;
    private ImageView ic_check_px;
    private LinearLayout ll_fapiao_message_px;
    private ImageView ic_check_hy;
    private LinearLayout ll_fapiao_message_hy;
    private String fapiaoMessage;
    private View inflate;
    private LinearLayout ll_check_personal;
    private LinearLayout ll_check_company;
    private ImageView ic_check_personal;
    private ImageView ic_check_company;
    private View fapiaoTypeView;
    private ImageView ic_check_dianzi;
    private LinearLayout ll_dianzi_fapiao;
    private ImageView ic_check_zengzhi;
    private LinearLayout ll_zengzhi_fapiao;
    private String order_money;
    private ZengZhiShuiBean zengZhiShuiBean;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_update_fa_piao;
    }

    @Override
    protected void initView() {
        loading_layout.setStatus(LoadingLayout.SUCCESS);
        if (!TextUtils.isEmpty(getIntent().getStringExtra("order_id")) && !TextUtils.isEmpty(getIntent().getStringExtra("order_sn")) && !TextUtils.isEmpty(getIntent().getStringExtra("order_money"))) {
            tvTitle.setText("申请发票");
            tvSubmitOrder.setText("提交");
            order_sn = getIntent().getStringExtra("order_sn");
            order_id = getIntent().getStringExtra("order_id");
            order_money = getIntent().getStringExtra("order_money");
            et_fapiao_sn.setText(order_sn);
            et_fapiao_sn.setFocusable(false);
        } else if (!TextUtils.isEmpty(getIntent().getStringExtra("order_id")) && !TextUtils.isEmpty(getIntent().getStringExtra("order_type"))) {
            order_id = getIntent().getStringExtra("order_id");
            order_type = getIntent().getStringExtra("order_type");
            tvTitle.setText("修改发票信息");
            tvSubmitOrder.setText("确认修改");
            //发票详情
            faPiaoDetails();
        }

        faopiaoFindViewById();

        //电子发票回显
        fapiaoShow();



    }

    private void fapiaoShow() {
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("token", getSharedPreferencesUtil().getLogintoken());
            //历史单位开票信息请求
            final GetHistoryInvoiceApi getHistoryInvoiceApi = new GetHistoryInvoiceApi(UpdateFaPiaoActivity.this, simpleOnNextListenerDianZi);
            getHistoryInvoiceApi.setCache(false);
            getHistoryInvoiceApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(getHistoryInvoiceApi);
        }
    }

    //历史单位开票信息回调
    HttpOnNextListener simpleOnNextListenerDianZi = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(UpdateFaPiaoActivity.this, subjects.getMsg());
                return;
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            GetComanyInvoiceBean getComanyInvoiceBean = (GetComanyInvoiceBean) subjects.getData();

            if (getComanyInvoiceBean != null) {
                etCompanyName.setText(getComanyInvoiceBean.getInvPayee() + "");
                etShuiTel.setText(getComanyInvoiceBean.getInvPayeeNumber() + "");
                etCompanyAddress.setText(getComanyInvoiceBean.getCompanyAddress() + "");
                etBankTel.setText(getComanyInvoiceBean.getBlank() + "");
                etEmail.setText(getComanyInvoiceBean.getEmail() + "");
                etPhoneTel.setText(getComanyInvoiceBean.getTel());
            }


        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    private void faopiaoFindViewById() {


        //发票信息
        fapiaoMessageView = LayoutInflater.from(UpdateFaPiaoActivity.this).inflate(R.layout.fapiao_content, null);
        rl_close = fapiaoMessageView.findViewById(R.id.rl_close);
        ic_check_px = fapiaoMessageView.findViewById(R.id.ic_check_px);
        ll_fapiao_message_px = fapiaoMessageView.findViewById(R.id.ll_fapiao_message_px);
        ic_check_hy = fapiaoMessageView.findViewById(R.id.ic_check_hy);
        ll_fapiao_message_hy = fapiaoMessageView.findViewById(R.id.ll_fapiao_message_hy);
        fapiaoMessage = tv_fapiao_message.getText().toString().trim();

        //电子发票类型
        inflate = LayoutInflater.from(UpdateFaPiaoActivity.this).inflate(R.layout.dianzi_fapiao_type, null);
        ll_check_personal = inflate.findViewById(R.id.ll_check_personal);
        ll_check_company = inflate.findViewById(R.id.ll_check_company);
        ic_check_personal = inflate.findViewById(R.id.ic_check_personal);
        ic_check_company = inflate.findViewById(R.id.ic_check_company);
        RelativeLayout rl_close_dianzi_type = inflate.findViewById(R.id.rl_close);
        rl_close_dianzi_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        //  发票类型
        fapiaoTypeView = LayoutInflater.from(UpdateFaPiaoActivity.this).inflate(R.layout.fapiao_type, null);
        RelativeLayout rl_close_fapiao_type = fapiaoTypeView.findViewById(R.id.rl_close);
        ic_check_dianzi = fapiaoTypeView.findViewById(R.id.ic_check_dianzi);
        ll_dianzi_fapiao = fapiaoTypeView.findViewById(R.id.ll_dianzi_fapiao);
        ic_check_zengzhi = fapiaoTypeView.findViewById(R.id.ic_check_zengzhi);
        ll_zengzhi_fapiao = fapiaoTypeView.findViewById(R.id.ll_zengzhi_fapiao);
        rl_close_fapiao_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

    }

    private void faPiaoDetails() {
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            Map<String, String> paramsBody = new HashMap<>();
            paramsBody.put("token", getSharedPreferencesUtil().getLogintoken());
            paramsBody.put("targetId", order_id);
//            发票来源：1：订单发票 (软件服务/元器件)2：充值发票 3：培训订单发票4：标准订单发票
            paramsBody.put("invFrom", "1");
            final InvoiceDetailByTargetIdApi invoiceDetailByTargetIdApi = new InvoiceDetailByTargetIdApi(this, simpleOnNextListener);
            invoiceDetailByTargetIdApi.setCache(false);
            invoiceDetailByTargetIdApi.setMap(paramsBody);
            HttpManager.getInstance().doHttpDeal(invoiceDetailByTargetIdApi);

            loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(invoiceDetailByTargetIdApi);
                    loading_layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    onBackPressed();
                }
            });
            loading_layout.setStatus(LoadingLayout.LOADING);
        } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }
    }

    @Override
    protected void addListener() {
        ll_dianzifapiao_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //电子发票类型选择
                dianZiFaPiaoType();


            }
        });
        ll_fapiao_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发票类型选择
                faPiaoType();
            }
        });
        ll_fapiao_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发票信息选择
                fapiaoMessage();
            }
        });
        tvSubmitOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //添加或修改发票
                updateFaPiao();
            }
        });

    }

    private void fapiaoMessage() {

        commonPop(fapiaoMessageView);
        if (fapiaoMessage.equals("培训费")) {
            ic_check_px.setSelected(true);
            ic_check_hy.setSelected(false);
        } else if (fapiaoMessage.equals("会议费")) {
            ic_check_px.setSelected(false);
            ic_check_hy.setSelected(true);
        } else {
            ic_check_px.setSelected(true);
            ic_check_hy.setSelected(false);
        }
        rl_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        ll_fapiao_message_px.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ic_check_px.setSelected(true);
                ic_check_hy.setSelected(false);
                tv_fapiao_message.setText("培训费");

            }
        });
        ll_fapiao_message_hy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ic_check_px.setSelected(false);
                ic_check_hy.setSelected(true);
                tv_fapiao_message.setText("会议费");
            }
        });
    }

    private void updateFaPiao() {

       /* 电子发票（invType=1）
        个人（invPayeeType=1）时
        invPayee：个人（单位名称，直接传参个人就行，必填）
        invContent：商品明细
        tel：18810421278（必填）
        email：5457@qq.com（非必填）

        电子发票单位（invPayeeType=2）时
        invPayee：中航（单位名,必填）
        invPayeeNumber（纳税人识别号，非必填，如果填写需校验格式，15或者18位只能有大写子母和数字组成   /^[0-9A-Z]*$/g）
        companyAddress：（地址及电话，非必填）
        bank：工行  62717011055545（开户行及账号，非必填）
        invContent：商品明细
        tel：18810421278（必填）
        email：5457@qq.com（非必填）

        增值税专用发票（invType=2）
        ticketId:(增票资质id，必填)
        invPayee：中航（单位名,必填）
        invPayeeNumber（纳税人识别号，非必填，如果填写需校验格式，15或者18位只能有大写子母和数字组成   /^[0-9A-Z]*$/g）
        address：（注册地址，必填）
        mobile：（注册电话，必填）
        bank：工行 （开户行，必填）
        blankNo:621788221(银行账号,必填)
        invContent：商品明细
        tel：18810421278（必填）
        email：5457@qq.com（非必填）

        申请发票时需要传以下两个参数
        targetId：订单ID或充值记录ID
        target：订单编号或者充值编号*/


        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            String fapiaoType = et_fapiao_type.getText().toString().trim();
            String fapiaoSn = et_fapiao_sn.getText().toString().trim();
            String faPiaoTaiTou = tvFapiaoTaitou.getText().toString().trim();
            String companyName = etCompanyName.getText().toString().trim();
            String shuiTel = etShuiTel.getText().toString().trim();
            String bankTel = etBankTel.getText().toString().trim();
            String companyAddress = etCompanyAddress.getText().toString().trim();
            String fapiao_message = tv_fapiao_message.getText().toString();
            String phoneTel = etPhoneTel.getText().toString().trim();
            String email = etEmail.getText().toString().trim();
            String registAddressZengzhi = etRegistAddressZengzhi.getText().toString().trim();
            String registPhoneZengzhi = etRegistPhoneZengzhi.getText().toString().trim();
            String bankNameZengzhi = etBankNameZengzhi.getText().toString().trim();
            String bankTelZengzhi = etBankTelZengzhi.getText().toString().trim();
            String fapiao_type = tv_fapiao_type.getText().toString().trim();
            String fapiaoTaitou = tvFapiaoTaitou.getText().toString().trim();

            Pattern pattern = Pattern.compile("^[0-9A-Z]*$");


            Map<String, String> paramBody = new HashMap<>();
            if (fapiao_type.equals("电子发票") && fapiaoTaitou.equals("个人")) {
                //必填
                paramBody.put("invType", "3");
                paramBody.put("invPayeeType", "1");
                if (TextUtils.isEmpty(fapiao_message)) {
                    ToastTools.showToast(this, "请填写发票明细");
                    return;
                }
                paramBody.put("invContent", fapiao_message);
                if (TextUtils.isEmpty(phoneTel)) {
                    ToastTools.showToast(this, "请填写手机号");
                    return;
                }
                paramBody.put("tel", phoneTel);

                paramBody.put("invPayee", "个人");
                //非必填
                if (!TextUtils.isEmpty(email) && !email.equals("null")) {
                    paramBody.put("email", email);
                }
            } else if (fapiao_type.equals("电子发票") && fapiaoTaitou.equals("单位")) {
                //必填
                paramBody.put("invType", "3");
                paramBody.put("invPayeeType", "2");
                if (TextUtils.isEmpty(companyName)) {
                    ToastTools.showToast(this, "请填写单位名称");
                    return;
                }
                paramBody.put("invPayee", companyName);
                if (TextUtils.isEmpty(phoneTel)) {
                    ToastTools.showToast(this, "请填写手机号");
                    return;
                }
                paramBody.put("tel", phoneTel);

                //非必填
                if (!TextUtils.isEmpty(shuiTel) && !shuiTel.equals("null") && pattern.matcher(shuiTel).matches()) {
                    paramBody.put("invPayeeNumber", shuiTel);
                }
                if (!TextUtils.isEmpty(companyAddress) && !companyAddress.equals("null")) {
                    paramBody.put("companyAddress", companyAddress);
                }
                if (!TextUtils.isEmpty(bankTel) && !bankTel.equals("null")) {
                    paramBody.put("bank", bankTel);
                }
                if (!TextUtils.isEmpty(fapiao_message) && !fapiao_message.equals("null")) {
                    paramBody.put("invContent", fapiao_message);
                }
                if (!TextUtils.isEmpty(email) && !email.equals("null")) {
                    paramBody.put("email", email);
                }
            } else if (fapiao_type.equals("增值税专用发票")) {
                //必填
                paramBody.put("invType", "2");
               /* if (TextUtils.isEmpty(getTICKET_ID())) {
                    ToastTools.showToast(this, "增票资质id");
                    return;
                }*/
                paramBody.put("ticketId", "");
                if (TextUtils.isEmpty(companyName)) {
                    ToastTools.showToast(this, "请填写公司名称");
                    return;
                }
                paramBody.put("invPayee", companyName);
                if (TextUtils.isEmpty(registAddressZengzhi)) {
                    ToastTools.showToast(this, "请填写注册地址");
                    return;
                }
                paramBody.put("address", registAddressZengzhi);
                if (TextUtils.isEmpty(registPhoneZengzhi)) {
                    ToastTools.showToast(this, "请填写注册电话");
                    return;
                }
                paramBody.put("mobile", registAddressZengzhi);
                if (TextUtils.isEmpty(bankNameZengzhi)) {
                    ToastTools.showToast(this, "请填写开户行");
                    return;
                }
                paramBody.put("bank", bankNameZengzhi);
                if (TextUtils.isEmpty(bankTelZengzhi)) {
                    ToastTools.showToast(this, "请填写银行账号");
                    return;
                }
                paramBody.put("blankNo", bankTelZengzhi);
                if (TextUtils.isEmpty(fapiao_message)) {
                    ToastTools.showToast(this, "请填写发票明细");
                    return;
                }
                paramBody.put("invContent", fapiao_message);
                if (TextUtils.isEmpty(phoneTel)) {
                    ToastTools.showToast(this, "请填写手机号");
                    return;
                }
                paramBody.put("tel", phoneTel);
                //非必填
                if (!TextUtils.isEmpty(shuiTel) && !shuiTel.equals("null") && pattern.matcher(shuiTel).matches()) {
                    paramBody.put("invPayeeNumber", shuiTel);
                }
                if (!TextUtils.isEmpty(email) && !email.equals("null")) {
                    paramBody.put("email", email);
                }
            }

            paramBody.put("token", getSharedPreferencesUtil().getLogintoken());
            paramBody.put("targetId", order_id);
            paramBody.put("target", order_sn);
            paramBody.put("tax", order_money);
            paramBody.put("status", "3");
           /* if (faPiaoTaiTou.equals("单位")) {
                if (TextUtils.isEmpty(fapiaoType)) {
                    ToastTools.showToast(this, "发票类型不能为空");
                } else if (TextUtils.isEmpty(fapiaoSn)) {
                    ToastTools.showToast(this, "订单编号不能为空");
                } else if (TextUtils.isEmpty(faPiaoTaiTou)) {
                    ToastTools.showToast(this, "发票抬头不能为空");
                } else if (TextUtils.isEmpty(companyName)) {
                    ToastTools.showToast(this, "公司名称不能为空");
                }
            } else if (faPiaoTaiTou.equals("个人")) {

            }
            Map<String, String> paramsBody = new HashMap<>();
            paramsBody.put("token", getSharedPreferencesUtil().getLogintoken());

            paramsBody.put("invId", fapiaoSn);

//            2：增值税专用发票3 : 电子发票
            if (fapiaoType.equals("增值税专用发票")) {
                paramsBody.put("invType", "2");
            } else if (fapiaoType.equals("电子发票")) {
                paramsBody.put("invType", "3");
            }

//            发票抬头类型：1：个人 2：单位
            if (faPiaoTaiTou.equals("单位")) {
                paramsBody.put("invPayeeType", "2");
            } else if (faPiaoTaiTou.equals("个人")) {
                paramsBody.put("invPayeeType", "1");
            }

            paramsBody.put("invPayee", companyName);
            paramsBody.put("invPayeeNumber", shuiTel);
            paramsBody.put("bank", bankTel);
            paramsBody.put("invContent", "");
            paramsBody.put("mobile", "");
            paramsBody.put("address", "");
            paramsBody.put("ticketId", "");*/
            final AddOrUpdateInvoiceApi addOrUpdateInvoiceApi = new AddOrUpdateInvoiceApi(this, simpleOnNextListenerAddOrUpdate);
            addOrUpdateInvoiceApi.setCache(false);
            addOrUpdateInvoiceApi.setMap(paramBody);
            HttpManager.getInstance().doHttpDeal(addOrUpdateInvoiceApi);

            loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(addOrUpdateInvoiceApi);
                    loading_layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    onBackPressed();
                }
            });
            loading_layout.setStatus(LoadingLayout.LOADING);
        } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }
    }

    private void faPiaoType() {

        commonPop(fapiaoTypeView);
        String faPiaoType = tv_fapiao_type.getText().toString().trim();
        if (faPiaoType.equals("电子发票")) {
            ic_check_dianzi.setSelected(true);
            ic_check_zengzhi.setSelected(false);
        } else if (faPiaoType.equals("增值税专用发票")) {
            ic_check_dianzi.setSelected(false);
            ic_check_zengzhi.setSelected(true);
        } else {
            ic_check_dianzi.setSelected(true);
            ic_check_zengzhi.setSelected(false);
        }


        ll_dianzi_fapiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fapiao_type = tv_fapiao_type.getText().toString().trim();
                String fapiaoTaitou = tvFapiaoTaitou.getText().toString().trim();
                ic_check_dianzi.setSelected(true);
                ic_check_zengzhi.setSelected(false);
                tv_fapiao_type.setText("电子发票");
                if (fapiaoTaitou.equals("个人")) {
                    llCompany.setVisibility(View.GONE);
                    llAddressPhoneZengzhi.setVisibility(View.GONE);
                    llBankBanktelZengzhi.setVisibility(View.GONE);
                    llCompanyAddress.setVisibility(View.GONE);
                    llBankTel.setVisibility(View.GONE);
                } else if (fapiaoTaitou.equals("单位")) {
                    llCompany.setVisibility(View.VISIBLE);
                    llAddressPhoneZengzhi.setVisibility(View.GONE);
                    llBankBanktelZengzhi.setVisibility(View.GONE);
                    llCompanyAddress.setVisibility(View.VISIBLE);
                    llBankTel.setVisibility(View.VISIBLE);
                }
                ll_dianzifapiao_type.setVisibility(View.VISIBLE);

            }
        });
        ll_zengzhi_fapiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ic_check_dianzi.setSelected(false);
                ic_check_zengzhi.setSelected(true);
                tv_fapiao_type.setText("增值税专用发票");
                llCompany.setVisibility(View.VISIBLE);
                ll_dianzifapiao_type.setVisibility(View.GONE);
                llBankTel.setVisibility(View.GONE);
                llCompanyAddress.setVisibility(View.GONE);
                llBankBanktelZengzhi.setVisibility(View.VISIBLE);
                llAddressPhoneZengzhi.setVisibility(View.VISIBLE);

                //增票回显
                zengpiaoShow();

            }
        });

    }

    private void zengpiaoShow() {
        //增票资质信息请求
        ZengZhiShuiApi zengZhiShuiApi = new ZengZhiShuiApi(UpdateFaPiaoActivity.this, simpleOnNextListenerZengZhi);
        zengZhiShuiApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(zengZhiShuiApi);
    }
    //增票资质信息回调
    HttpOnNextListener simpleOnNextListenerZengZhi = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {

            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(UpdateFaPiaoActivity.this, subjects.getMsg());
                return;
            }
            zengZhiShuiBean = (ZengZhiShuiBean) subjects.getData();

            if (zengZhiShuiBean != null) {

                etCompanyName.setText(zengZhiShuiBean.getCompanyName() + "");
                etShuiTel.setText(zengZhiShuiBean.getIdentificationNumber() + "");
                etRegistAddressZengzhi.setText(zengZhiShuiBean.getRegAddress() + "");
                etRegistPhoneZengzhi.setText(zengZhiShuiBean.getRegMobile() + "");
                etBankNameZengzhi.setText(zengZhiShuiBean.getBank());
                etBankTelZengzhi.setText(zengZhiShuiBean.getBlankNo() + "");
            }

        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    private void dianZiFaPiaoType() {

        commonPop(inflate);
        String fapiaoTaiTou = tvFapiaoTaitou.getText().toString().trim();
        if (fapiaoTaiTou.equals("单位")) {
            ic_check_company.setSelected(true);
            ic_check_personal.setSelected(false);
        } else if (fapiaoTaiTou.equals("个人")) {
            ic_check_company.setSelected(false);
            ic_check_personal.setSelected(true);
        } else {
            ic_check_company.setSelected(false);
            ic_check_personal.setSelected(true);
        }


        ll_check_company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ic_check_company.setSelected(true);
                ic_check_personal.setSelected(false);

                tvFapiaoTaitou.setText("单位");


                llCompany.setVisibility(View.VISIBLE);
                llAddressPhoneZengzhi.setVisibility(View.GONE);
                llBankBanktelZengzhi.setVisibility(View.GONE);
                llCompanyAddress.setVisibility(View.VISIBLE);
                llBankTel.setVisibility(View.VISIBLE);

            }
        });
        ll_check_personal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ic_check_company.setSelected(false);
                ic_check_personal.setSelected(true);

                tvFapiaoTaitou.setText("个人");


                llCompany.setVisibility(View.GONE);
                llAddressPhoneZengzhi.setVisibility(View.GONE);
                llBankBanktelZengzhi.setVisibility(View.GONE);
                llCompanyAddress.setVisibility(View.GONE);
                llBankTel.setVisibility(View.GONE);
            }
        });

    }

    private void commonPop(View view) {
        popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.ll_root), Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            // 在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow()
                        .getAttributes();
                lp.alpha = 1f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);
            }
        });
    }

    //权威评测
    HttpOnNextListener simpleOnNextListenerAddOrUpdate = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
//            listData.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(UpdateFaPiaoActivity.this, subjects.getMsg());
                return;
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            Intent intent = new Intent();
            setResult(RESULT_OK,intent);
            finish();


        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    //权威评测
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
//            listData.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(UpdateFaPiaoActivity.this, subjects.getMsg());
                return;
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            FaPiaoDetailsBean faPiaoDetailsBean = (FaPiaoDetailsBean) subjects.getData();
            if (faPiaoDetailsBean != null) {
                et_fapiao_sn.setText(faPiaoDetailsBean.getTarget() + "");
                if (faPiaoDetailsBean.getInvType() == 2) {
                    et_fapiao_type.setText("增值税专用发票");
                } else if (faPiaoDetailsBean.getInvType() == 3) {
                    et_fapiao_type.setText("电子发票");
                }
                etOrderContent.setText(faPiaoDetailsBean.getInvContent());
                if (faPiaoDetailsBean.getInvPayeeType() == 2) {
                    tvFapiaoTaitou.setText("个人");
                } else if (faPiaoDetailsBean.getInvType() == 3) {
                    tvFapiaoTaitou.setText("单位");
                }
                etPhoneTel.setText(faPiaoDetailsBean.getMobile() + "");
                etEmail.setText(faPiaoDetailsBean.getEmail() + "");

            }


        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

}
